package com.leave.mos.service.impl;

import com.leave.mos.constant.TablenameConstant;
import com.leave.mos.entity.Order;
import com.leave.mos.entity.QueryParam;
import com.leave.mos.service.find.QueryParamService;
import com.mongodb.MongoException;
import org.bson.Document;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.DocumentCallbackHandler;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Field;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Query查询条件的使用   查询 排序 分页 字段获取
 **/
@Service
public class QueryParamServiceImpl implements QueryParamService {

    @Resource
    private MongoTemplate mongoTemplate;

    //分页 排序
    @Override
    public List<Order> findQuery1(QueryParam queryParam){
        //分页查询
        Query query = new Query();
        //跳过多少行数据
        query.skip(queryParam.getSkipNum());
        //获取多少行数据
        query.limit(queryParam.getPageSize());

        //添加查询排序
        query.with(Sort.by(Sort.Direction.DESC,TablenameConstant.Order.dealName));

        System.out.println(query.isSorted());

        List<Order> orders = mongoTemplate.find(query,Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    //查询所有的数据
    @Override
    public List<Order> findQuery2(QueryParam queryParam){
        //分页查询
        Query query = new Query();

        List<Order> orders = mongoTemplate.find(query,Order.class, TablenameConstant.Order.TableName);
        return orders;
    }


    @Override
    public List<Order> findQuery3(QueryParam queryParam){
        //设置分页信息 并排序
        PageRequest pageable = PageRequest.of(queryParam.getPageNum(),queryParam.getPageSize(),
                Sort.by(Sort.Direction.DESC,TablenameConstant.Order.dealName));
        //查询条件
        Query query = new Query();
        query.with(pageable);

        List<Order> orders = mongoTemplate.find(query,Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    @Override
    public List<Order> findQuery4(QueryParam queryParam) throws ParseException {

        //生成一个时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //Date date = format.parse(queryParam.getCreatTime());
        //查询 必须的字段查询出来，不需要的不查询。
        Query query = new Query();
        Field fields = query.fields();
        fields.include(TablenameConstant.Order.creatTime,
                TablenameConstant.Order.dealName);

        List<Order> orders = mongoTemplate.find(query,Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    //使用query方法查询数据。
    @Override
    public List<Order> query5(QueryParam queryParam) throws ParseException {
        //查询 必须的字段查询出来，不需要的不查询。
        Query query = new Query();
        query.addCriteria(Criteria.where(TablenameConstant.Order.id).is(queryParam.getId()));

        List<Order> orders = mongoTemplate.query(Order.class).matching(query).all();
        return orders;
    }

    //使用query方法查询数据。
    @Override
    public List<Order> query6(QueryParam queryParam) throws ParseException {
        //查询 必须的字段查询出来，不需要的不查询。
        Query query = new Query();
        query.addCriteria(Criteria.where(TablenameConstant.Order.id).is(queryParam.getId()));

        mongoTemplate.executeQuery(query,TablenameConstant.Order.TableName,new DocumentCallbackHandlerImpl());
        return null;
    }


}

//自定义callback
class DocumentCallbackHandlerImpl implements DocumentCallbackHandler{

    @Override
    public void processDocument(Document document) throws MongoException, DataAccessException {
        System.out.println(document.toJson());
    }
}
